//
//  ArchiveHandleSingle.m
//  iNews
//
//  Created by lanou3g on 14-12-12.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "ArchiveHandleSingle.h"
#import "DetailModel.h"
#import "DetailArchiveModel.h"

#define kArticlePath @"/article.plist"
#define kPicturePath @"/picture.plist"

static ArchiveHandleSingle *share = nil;

@implementation ArchiveHandleSingle

- (void)dealloc
{
    [_articleArray release];
    [_pictureArray release];
    
    [super dealloc];
}

+ (ArchiveHandleSingle *)archiveSingle
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[ArchiveHandleSingle alloc] init];
    });
    return share;
}

//收藏文章到 /article.plist
- (void)archiveModel:(DetailModel *)model
{
    NSMutableArray *tempArr;
    

    [self.articleArray removeAllObjects];
    [self unachiveArray];

    int t = 0;
    if (self.articleArray.count == 0) {
        tempArr = [NSMutableArray array];
        //判断是否进行归档的依据
     
    }else{
        
        tempArr = [NSMutableArray arrayWithArray:self.articleArray];
        
        for (DetailModel *m in tempArr) {
            if ([m.docid isEqualToString:model.docid]) {
                
                NSLog(@"已经收藏了");
                //代表不能归档
                t = 1;
                break;
            }
        }
    }
    
    if (!t) {
        
        //进行归档
        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSString *filePath = [documentPath stringByAppendingString:kArticlePath];
        
        [tempArr addObject:model];
        self.articleArray = [NSMutableArray arrayWithArray:tempArr];
        [NSKeyedArchiver archiveRootObject:tempArr toFile:filePath];
        
        //通知中心通知更多界面进行更新界面
        [[NSNotificationCenter defaultCenter] postNotificationName:@"articleArray" object:nil];
        
     //   NSLog(@"归档成功");
     //   NSLog(@"%@",self.articleArray);
    }
}


//反归档,查看,读取
- (void)unachiveArray
{
    
    //进行反归档
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [documentPath stringByAppendingString:kArticlePath];

    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    self.articleArray = [NSMutableArray arrayWithArray:array];
    
}


//删除数据 (取消收藏)
- (void)deleteDMValue:(DetailModel *)model
{
  //  [self.articleArray removeAllObjects];
    
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [documentPath stringByAppendingString:kArticlePath];
    
//    NSString *s = model.docid;
//    NSLog(@"%@",s);

    
    //先取到数据
    [self unachiveArray];
    
    if (self.articleArray.count == 0) {
        NSLog(@"没有收藏的");
        
    }else{
        
        for (DetailModel *m in self.articleArray) {
            
            if ([m.docid isEqualToString:model.docid]) {
                
                [self.articleArray removeObject:m];
                break;
            }
            
        }
        
        [NSKeyedArchiver archiveRootObject:self.articleArray toFile:filePath];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"articleArray" object:nil];
        
    }
}



#pragma mark 归档图集的三个方法
//归档图片,model类不同
- (void)archivePicture:(DetailArchiveModel *)model
{
    
    NSMutableArray *arr;
    
    [self searchPicture];
    
    int t = 0;
    if (self.pictureArray.count == 0) {
        
        arr = [NSMutableArray array];
        //判断是否进行归档的依据
        t = 0;
    
    }else{
        
        arr = [NSMutableArray arrayWithArray:self.pictureArray];
        
        for (DetailArchiveModel *d in arr) {
            if ([d.postid isEqualToString:model.postid]) {
                
                NSLog(@"已经收藏了");
                //代表不能归档
                t = 1;
                break;
            }
        }
    }
    
    if (!t) {
        
        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSString *picturePath = [documentPath stringByAppendingString:kPicturePath];
        
        //归档数组
        [arr addObject:model];
        [NSKeyedArchiver archiveRootObject:arr toFile:picturePath];
        self.pictureArray = [NSMutableArray arrayWithArray:arr];
        
        //去收藏里面添加观察者以便及时更新ui;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pictureArray" object:nil];
        
     //   NSLog(@"收藏图片了");
      //  NSLog(@"沙盒里面的图片数组:%@",self.pictureArray);
        
    }

}

//删除图片(取消收藏)
- (void)deletePictureView:(DetailArchiveModel *)model
{
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [documentPath stringByAppendingString:kPicturePath];
    
    //先取到数据
    [self searchPicture];
    
    if (self.pictureArray.count == 0) {
        
        NSLog(@"没有收藏的");
        
    }else{
        
        for (DetailArchiveModel *m in self.pictureArray) {
            
            if ([m.postid isEqualToString:model.postid]) {
                
                [self.pictureArray removeObject:m];
                break;
            }
            
        }
    
        [NSKeyedArchiver archiveRootObject:self.pictureArray toFile:filePath];
        
      //  NSLog(@"删除图集了");
        //添加观察者观察
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pictureArray" object:nil];
    }

}

//查找图集
- (void)searchPicture
{
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [documentPath stringByAppendingString:kPicturePath];
    
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    self.pictureArray = [NSMutableArray arrayWithArray:array];
}









@end
